
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Identity.Contexts;
using Identity.Service;
using Identity.Interface;
using MyEncrytExensions;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Identity.Models;
using Microsoft.AspNetCore.Authorization;
using Identity.AdminSecurityExtensions;

namespace Identity.Extensions;

    public static class ServiceCollectionExtensions
    {

        public static IServiceCollection AddMyIdentity(this IServiceCollection services, IConfiguration configuration)
        {
        
            var encryptString = configuration.GetConnectionString("SysConnection") ?? throw new InvalidOperationException("故障未能提供人员信息数据库连接字符串");
         

            // Console.WriteLine("Identity\n"+"Data Source=./Identity.db".EncryptText()) ;
            var databaseType = configuration["DatabaseType"] ?? throw new InvalidOperationException("故障未能提供数据库类型");

            services.AddTransient<IAdminService,AdminService>();
            services.AddTransient<INavigation,NavigationService>();
            services.AddTransient<IRole,RoleService>();
            services.AddTransient<IUser,UserService>();
            services.AddScoped<MyIdentityContext>();

            switch (databaseType.ToLower())
            {
                case "sqlite":
                    services.AddDbContextFactory<SysContext>(options =>
                        options.UseSqlite(encryptString.DecryptText()));
                    break;
                case "mysql":
                    services.AddDbContextFactory<SysContext>(options =>
                        options.UseMySql(encryptString.DecryptText(), new MySqlServerVersion(new Version(8, 0, 23))));
                    break;
                default:
                    throw new InvalidOperationException($"不支持的数据库类型: {databaseType}");
            }
            services.AddCascadingAuthenticationState();
            services.AddScoped<IdentityUserAccessor>();
            services.AddScoped<IdentityRedirectManager>();
            services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
            services.AddAuthentication(options =>
                {
                    options.DefaultScheme = IdentityConstants.ApplicationScheme;
                    options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
                })
                .AddIdentityCookies();
            services.AddDatabaseDeveloperPageExceptionFilter();
 
            services.AddIdentityCore<User>(options =>
            {
                options.Password.RequireDigit = true;
                options.Password.RequireLowercase = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequiredLength = 6;
                options.Password.RequiredUniqueChars = 1;
                options.SignIn.RequireConfirmedAccount = false;
                options.SignIn.RequireConfirmedEmail = false;
            })
            .AddRoles<Role>() // 添加角色支持
            .AddEntityFrameworkStores<SysContext>() // 配置 EF Core 存储
            .AddSignInManager() // 注册 SignInManager
            .AddClaimsPrincipalFactory<CustomUserClaimsPrincipalFactory>() // 注册自定义 ClaimsPrincipalFactory
            .AddDefaultTokenProviders(); 
            // Console.WriteLine("CustomUserClaimsPrincipalFactory 已注册");

            services.AddSingleton<IEmailSender<User>, IdentityNoOpEmailSender>();
            return services;
        }
 
 
    }
